<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="场地" prop="positionId">
            <el-select v-model="queryParams.positionId" placeholder="请选择" @keyup.enter="handleQuery" clearable="true">
              <el-option
                v-for="item in positionList"
                :key="item.positionId"
                :label="item.name"
                :value="item.positionId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="提现状态" prop="withdrawStatus">
            <el-select v-model="queryParams.withdrawStatus" placeholder="请选择状态" clearable>
              <el-option
                v-for="dict in manager_withdraw_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="审核状态" prop="auditStatus">
            <el-select v-model="queryParams.auditStatus" placeholder="请选择审核状态" clearable>
              <el-option
                v-for="dict in manager_audit_status"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="managerWithdrawList" border="true">
        <el-table-column label="#" type="index" width="55" align="center" fixed="left"/>
        <el-table-column label="提现ID" align="center" prop="managerWithdrawId" v-if="false" />
        <el-table-column label="管理员ID" align="center" prop="sysUserId" v-if="false"/>
        <el-table-column label="用户名称" align="center" prop="sysUserName" />
        <el-table-column label="场地ID" align="center" prop="positionId" v-if="false"/>
        <el-table-column label="场地名称" align="center" prop="positionName"/>
        <el-table-column label="提现单号" align="center" prop="withdrawNo" width="180"/>
        <el-table-column label="提现金额" align="center" prop="withdrawAmount" />
        <el-table-column label="交易成功时间" align="center" prop="withdrawTime" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.withdrawTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="提现状态" align="center" prop="withdrawStatus">
          <template #default="scope">
            <dict-tag :options="manager_withdraw_status" :value="scope.row.withdrawStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="提现备注" align="center" prop="withdrawRemark" />
        <el-table-column label="审核状态" align="center" prop="auditStatus">
          <template #default="scope">
            <dict-tag :options="manager_audit_status" :value="scope.row.auditStatus"/>
          </template>
        </el-table-column>
        <el-table-column label="审核备注" align="center" prop="auditRemark" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="审核" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" :disabled="scope.row.auditStatus != 'wait'">审核</el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改代理商提现记录对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
      <el-form ref="managerWithdrawFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户" prop="sysUserName">
          <el-input v-model="form.sysUserName" placeholder="请输入管理员ID" disabled/>
        </el-form-item>
        <el-form-item label="场地" prop="positionName">
          <el-input v-model="form.positionName" placeholder="请输入场地ID" disabled/>
        </el-form-item>
        <el-form-item label="提现单号" prop="withdrawNo">
          <el-input v-model="form.withdrawNo" placeholder="请输入提现单号" disabled/>
        </el-form-item>
        <el-form-item label="提现金额" prop="withdrawAmount">
          <el-input v-model="form.withdrawAmount" placeholder="请输入提现金额" disabled/>
        </el-form-item>
        <el-form-item label="提现备注" prop="withdrawRemark">
          <el-input v-model="form.withdrawRemark" placeholder="请输入备注" disabled/>
        </el-form-item>
        <el-form-item label="审核状态" prop="auditStatus">
          <el-radio-group v-model="form.auditStatus" class="ml-4">
            <el-radio label="pass" size="large">通过</el-radio>
            <el-radio label="refund" size="large">拒绝</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审核备注" prop="auditRemark">
          <el-input v-model="form.auditRemark" placeholder="请输入审核备注" maxlength="10" show-word-limit/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ManagerWithdraw" lang="ts">
import {
  listManagerWithdraw,
  getManagerWithdraw,
  delManagerWithdraw,
  addManagerWithdraw,
  updateManagerWithdraw,
  withdrawAudit
} from '@/api/device/managerWithdraw';
import { ManagerWithdrawVO, ManagerWithdrawQuery, ManagerWithdrawForm } from '@/api/device/managerWithdraw/types';
import {allPosition} from "@/api/device/position";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { manager_audit_status, manager_withdraw_status } = toRefs<any>(proxy?.useDict('manager_audit_status', 'manager_withdraw_status'));

const managerWithdrawList = ref<ManagerWithdrawVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const managerWithdrawFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: ManagerWithdrawForm = {
  managerWithdrawId: undefined,
  sysUserId: undefined,
  sysUserName: undefined,
  positionId: undefined,
  positionName: undefined,
  withdrawNo: undefined,
  withdrawAmount: undefined,
  withdrawTime: undefined,
  withdrawStatus: undefined,
  withdrawRemark: undefined,
  auditStatus: undefined,
  auditRemark: undefined,
  remark: undefined
}
const data = reactive<PageData<ManagerWithdrawForm, ManagerWithdrawQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: 'create_time',
    isAsc: 'desc',
    sysUserId: undefined,
    positionId: undefined,
    withdrawNo: undefined,
    withdrawAmount: undefined,
    withdrawTime: undefined,
    withdrawStatus: undefined,
    withdrawRemark: undefined,
    auditStatus: undefined,
    auditRemark: undefined,
    params: {
    }
  },
  positionList:[],
  rules: {
    managerWithdrawId: [
      { required: true, message: "提现ID不能为空", trigger: "blur" }
    ],
    sysUserId: [
      { required: true, message: "管理员ID不能为空", trigger: "blur" }
    ],
    positionId: [
      { required: true, message: "场地ID不能为空", trigger: "blur" }
    ],
    withdrawNo: [
      { required: true, message: "提现单号不能为空", trigger: "blur" }
    ],
    withdrawAmount: [
      { required: true, message: "提现金额不能为空", trigger: "blur" }
    ],
    withdrawTime: [
      { required: true, message: "交易成功时间不能为空", trigger: "blur" }
    ],
    withdrawStatus: [
      { required: true, message: "状态：withdrawing 提现中,withdraw_success 提现成功,fail 失败,不能为空", trigger: "change" }
    ],
    auditStatus: [
      { required: true, message: "审核状态拒绝不能为空", trigger: "change" }
    ],
    auditRemark: [
      { required: true, message: "审核备注不能为空", trigger: "blur" }
    ],
    remark: [
      { required: true, message: "备注不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules,positionList } = toRefs(data);

const getPositionList = async () => {
  loading.value = true;
  const res = await allPosition();
  positionList.value = res.data;
  loading.value = false;
}

/** 查询代理商提现记录列表 */
const getList = async () => {
  loading.value = true;
  const res = await listManagerWithdraw(queryParams.value);
  managerWithdrawList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  managerWithdrawFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: ManagerWithdrawVO[]) => {
  ids.value = selection.map(item => item.managerWithdrawId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加代理商提现记录";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: ManagerWithdrawVO) => {
  reset();
  const _managerWithdrawId = row?.managerWithdrawId || ids.value[0]
  const res = await getManagerWithdraw(_managerWithdrawId);
  Object.assign(form.value, res.data);
  if (!form.value.auditStatus || form.value.auditStatus ==='wait'){
    form.value.auditStatus = 'pass'
  }
  dialog.visible = true;
  dialog.title = "审批";
}

/** 提交按钮 */
const submitForm = () => {
  managerWithdrawFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      await withdrawAudit(form.value).finally(() =>  buttonLoading.value = false);
      proxy?.$modal.msgSuccess("审批成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: ManagerWithdrawVO) => {
  const _managerWithdrawIds = row?.managerWithdrawId || ids.value;
  await proxy?.$modal.confirm('是否确认删除代理商提现记录编号为"' + _managerWithdrawIds + '"的数据项？').finally(() => loading.value = false);
  await delManagerWithdraw(_managerWithdrawIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('device/managerWithdraw/export', {
    ...queryParams.value
  }, `managerWithdraw_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
  getPositionList();
});
</script>
